import java.util.Hashtable;
import java.util.LinkedList;

public class Graph {
	Hashtable<Integer, Integer> weights = new Hashtable<Integer, Integer>();
	Hashtable<Integer, LinkedList<Integer>> connectionsFrom = new Hashtable<Integer, LinkedList<Integer>>();
	
	public void incrWeight(Integer id) {
		weights.put(id, weights.get(id) + 1);
	}
	
	public void addNode(Integer id) {
		weights.put(id, 0);
		connectionsFrom.put(id, new LinkedList<Integer>());
	}
	
	public void addEdge(Integer fromId, Integer toId)
	{	
		connectionsFrom.get(fromId).add(toId);
		connectionsFrom.get(toId).add(fromId);
	}
	
	public boolean existsNode(Integer id)
	{
		return connectionsFrom.containsKey(id);
	}
	
	public boolean existsEdge(Integer fromId, Integer toId)
	{
		if (!existsNode(fromId) || !existsNode(toId))
		{
			return false;
		}
		return connectionsFrom.get(fromId).contains(toId);
	}
	
	public int sumOfWeights(LinkedList<Integer> nodeSet)
	{
		int sum = 0;
		for (Integer node : nodeSet) {
			sum += weights.get(node);
		}
		return sum;
	}

}
